package hkyg.background.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hkyg.background.data.BackgroundMapper;
import hkyg.background.model.GeBytearray;
import hkyg.background.model.UserInfo;
import hkyg.background.model.WebContent;
import hkyg.util.HkygUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BackgroundService {
	@Autowired
	private  BackgroundMapper backMapper;
	
	@Transactional(rollbackFor=Exception.class)
	public boolean login(Map<String,Object> params) throws Exception{
		HkygUtil.md5Psd(params);//md5加密
		List<UserInfo> list = backMapper.getUserInfoList(params);
		if(list.size() > 0) {
			//修改最后登录时间
			UserInfo user = list.get(0);
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put("user_id", user.getUser_id());
			temp.put("last_login_time", new Date());
			backMapper.updateLastLoginTime(temp);
			HkygUtil.setAttribute("user", user.getUser_name());
			params.put("data", user.getAuthority());
			return true;
		}else {
			return false;
		}
	}
	
	//获取新闻数目
	public int getNewsCount(Map<String,Object> params) {
		return backMapper.getNewsCount(params);
	}
	//获取新闻列表
	public List<WebContent> getNewsList(Map<String,Object> params) {
		int count = getNewsCount(params);
		params.put("total", count);
		return backMapper.getNewsList(params);
	}
	//新增一条新闻
	public void addNews(Map<String,Object> params) {
		//创建人更新人及时间
		String userFromSession = (String)HkygUtil.getAttribute("user");
		Date now = new Date();
		params.put("create_user", userFromSession);
		params.put("modify_user", userFromSession);
		params.put("create_time", now);
		params.put("modify_time", now);
		backMapper.addNews(params);
	}
	//更改一条新闻
	public void updateNews(Map<String,Object> params) {
		String userFromSession = (String)HkygUtil.getAttribute("user");
		Date now = new Date();
		params.put("modify_user", userFromSession);
		params.put("modify_time", now);
		backMapper.updateNews(params);
	}
	//删除一条新闻
	public void delNews(Map<String,Object> params) {
		backMapper.delNews(params);
	}
	
	/**
	 * 获取用户数目
	 * @author donghj
	 * @param params
	 * @return
	 */
	public int getUserCount(Map<String,Object> params) {
		return backMapper.getUserCount(params);
	}
	/**
	 * 获取用户列表
	 * @author	donghj
	 * @param	params
	 * @return	List<UserInfo>
	 */
	public List<UserInfo> getUserList(Map<String,Object> params) {
		int count = getUserCount(params);
		params.put("total", count);
		return backMapper.getUserList(params);
	}
	/**
	 * 新增用户
	 * @author	donghj
	 * @param	params
	 */
	public void addUser(Map<String,Object> params) {
		HkygUtil.md5Psd(params);//md5加密
		//创建人更新人及时间
		String userFromSession = (String)HkygUtil.getAttribute("user");
		Date now = new Date();
		params.put("create_user", userFromSession);
		params.put("modify_user", userFromSession);
		params.put("create_time", now);
		params.put("modify_time", now);
		backMapper.addUser(params);
	}
	/**
	 * 更新用户
	 * @author	donghj
	 * @param	params
	 */
	public void updateUser(Map<String,Object> params) {
		HkygUtil.md5Psd(params);//md5加密
		String userFromSession = (String)HkygUtil.getAttribute("user");
		Date now = new Date();
		params.put("modify_user", userFromSession);
		params.put("modify_time", now);
		backMapper.updateUser(params);
	}
	/**
	 * 删除用户
	 * @author	donghj
	 * @param	params
	 */
	public void delUser(Map<String,Object> params) {
		backMapper.delUser(params);
	}
	/**
	 * 修改用户密码
	 * @author 杨笑天
	 */
	public boolean changePassword(Map<String,Object> params) {
		try {
			HkygUtil.md5Psd(params);
			List<UserInfo> userList = backMapper.getUserInfoList(params);
			if(userList.size() > 0) {
				UserInfo userInfo = userList.get(0);
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("user_id", userInfo.getUser_id());
				map.put("password", params.get("new_password"));
				HkygUtil.md5Psd(map);
				backMapper.changePassword(map);
				return true;
			} else {
				return false;
			}
		}catch(Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	/**
	 * 往数据仓库表ge_bytearray添加一条数据
	 * @author 杨笑天
	 */
	public void addGeBytearray(GeBytearray byteArray){
		backMapper.addByteArray(byteArray);
	}
	/**
	 * 获取ge_bytearray表中对应pg_id的内容
	 * @param pg_id 数据仓库中的主键
	 * @return Gebytearray对象
	 * @author 杨笑天
	 */
	public GeBytearray getGeBytearray(int pg_id){
		return backMapper.getByteArray(pg_id);
	}
}
